package com.hotel.project.system.customer.controller;

import com.hotel.framework.web.controller.BaseController;
import com.hotel.framework.web.domain.AjaxResult;
import com.hotel.framework.web.page.TableDataInfo;
import com.hotel.project.system.ReserveInfo;
import com.hotel.project.system.customer.service.CustomerService;
import com.hotel.project.system.guestroom.domain.GuestRoom;
import com.hotel.project.system.guestroom.service.IGuestRoomService;
import com.hotel.project.system.roomorders.domain.RoomOrders;
import com.hotel.project.system.roomorders.service.IRoomOrdersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zy
 * @date 2019-04-06 21:05
 **/
@Controller
@RequestMapping("/system/customer")
public class CustomerController extends BaseController {

    private String prefix = "system/customer";

    @Autowired
    private CustomerService customerService;
    @Autowired
    private IGuestRoomService guestRoomService;
    @Autowired
    private IRoomOrdersService roomOrdersService;

    @RequiresPermissions("system:customer:view")
    @GetMapping()
    public String getRoom() {
        return prefix + "/room";
    }

    @PostMapping("/room/list")
    @ResponseBody
    public TableDataInfo list(GuestRoom guestRoom) {
        startPage();
        List<GuestRoom> list = guestRoomService.selectGuestRoomList(guestRoom);
        return getDataTable(list);
    }

    @GetMapping("/reserve/{roomId}")
    public String reserve(@PathVariable("roomId") int roomId, ModelMap mmap) {
        GuestRoom room = guestRoomService.selectGuestRoomById(roomId);
        mmap.put("room", room);
        return prefix + "/reserve";
    }

    @PostMapping("/reserve")
    @ResponseBody
    public AjaxResult reserve(ReserveInfo reserveInfo) {
        return toAjax(customerService.reserve(reserveInfo.getRoomId(), reserveInfo.getStopDay(), getSysUser()));
    }

    @RequiresPermissions("system:customer:view")
    @GetMapping("/orders")
    public String getOrders() {
        return prefix + "/orders";
    }

    @PostMapping("/orders/list")
    @ResponseBody
    public TableDataInfo list(RoomOrders roomOrders) {
        roomOrders.setUserId(getUserId().intValue());
        startPage();
        List<RoomOrders> list = roomOrdersService.selectRoomOrdersList(roomOrders);
        return getDataTable(list);
    }

    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(int orderId) {
        return toAjax(customerService.cancel(orderId));
    }
}
